<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends CI_Controller {
  
    public function __construct()
    {
        parent::__construct();
        $this->layout->setLayout('backend');
         
        $this->load->library(array('session','form_validation','email'));           
        $this->load->helper(array('url','html'));
        $this->load->model('habilitaciones_model'); 
    }

    public function index()
    {   
        $data['perfil'] = $this->session->userdata('perfil');
        $perfil = $this->session->userdata('perfil');        
        print_r($perfil);
        if(empty($perfil))
        {
            redirect(base_url().'backend');
        }
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->form_validation->set_rules('down', 'Fuera de servicio', 'required');        
        $data['subtitle'] = 'Pre-Formulario de Habilitación Especial';  
        $data['username'] = $this->session->userdata('username');

        if ($this->form_validation->run() === FALSE)
        {
            $data['title'] = 'Dirección de Habilitaciones';   
            $data['estadoPagina'] = $this->habilitaciones_model->getPageState();            
            $data['aHabilitaciones'] =$this->habilitaciones_model->getHabilitaciones();  
            if ($data['estadoPagina']=='Deshabilitada') {
                $data['color'] = 'red';
            }else{
                $data['color'] = 'green';                
            }
            $this->layout->view('index',$data); 
        }
        else
        {              
            $down = $this->input->post('down'); 
            if ($down == 'Y') {
                      $estado = 'Deshabilitada';                      
            }else
            {
                $estado = 'Habilitada';        
            }
            $data = array('id' => '1','fueradeservicio' => $down, 'estado'=> $estado );            
            $this->habilitaciones_model->setState($data);
            // $data['aHabilitaciones'] =$this->habilitaciones_model->getHabilitaciones();
            $data['title'] = 'Dirección de Habilitaciones';         
            if ($down == 'Y') {
                $data['mensaje'] = 'La pagina ha sido deshabilitada.';                     
                $this->load->view('success',$data); 
            }else
            {
                $data['mensaje'] = 'La pagina ha sido habilitada.';                     
                $this->load->view('success',$data);     
            }                    
        }
    }

    public function setVisto($id,$username){
        $data = array('id' => $id,'vistopor' => $username); 
        $username = $this->session->userdata('username');
        $this->habilitaciones_model->setVistoPor($data);
        redirect(base_url().'backend/admin#myModal'.$id);
    }

    public function setUser(){  
        $usrType = $this->input->post('usrType');
        $usuario = $this->input->post('usuario');
        $password = $this->input->post('password');        
        $data = array(
               'username' => $usuario,
               'perfil' => $usrType ,            
               'password' => sha1($password)
               );
        $this->habilitaciones_model->setForm('users',$data);       
        redirect(base_url().'backend/admin#myModal'.$id); 
    }

    public function getTable(){                    
        $aTabla = $this->habilitaciones_model->getHabilitaciones();                
        $aData = json_encode($aTabla);                 
        print_r($aData);        
        return $aTabla;
    }    
}
      
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */